/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs425proj.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;

import cs425proj.model.*;
import java.sql.*;
import javax.servlet.RequestDispatcher;
/**
 *
 * @author prateekpatil
 */
public class cast extends HttpServlet {
    
     private Statement stm;
     private connectDB obj;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        obj = new connectDB();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            Connection con = obj.startConnection();
            stm =con.createStatement();
            String fname = request.getParameter("fname");
            String lname = request.getParameter("lname");
            int  movieid = Integer.parseInt(request.getParameter("movieid"));
            String type = request.getParameter("type");
            String qry = "insert into cast values(seq_cast_id.nextval ,?,?,?,?)";
            
            PreparedStatement pstmt = con.prepareStatement(qry);
            pstmt.setString(1, fname);
            pstmt.setString(2, lname);
            pstmt.setInt(3, movieid);
            pstmt.setString(4,type);
            int executeUpdate = pstmt.executeUpdate();
            
            stm= con.createStatement();
            
            if(request.getParameter("addmore")!= null){
                    ResultSet rs = stm.executeQuery("Select * from cast where movie_id='"+movieid+"'");
                    int movie_id =0;
                    while(rs.next()){
                            movie_id= rs.getInt(4);
                             }
                    obj.closeConnection();
                    String moviename = request.getParameter("moviename");
                    request.setAttribute("movieid", movie_id);
                    request.setAttribute("moviename", moviename);
                    String destination="/cast.jsp";
                    RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
                    rd.forward(request, response);
                    
            }else
            {       obj.closeConnection();
                    String destination="/staff.jsp";
                    RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
                    rd.forward(request, response);
                    
                
            }
            
            
        }catch(SQLException e){e.printStackTrace();} 
        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
